extends Marker2D

class_name Chair

@onready var world: = $"../../.."
@export var empty: bool = true
@onready var eat: = $EatArea
var man: Human
var food: Food
signal add_score
# Called when the node enters the scene tree for the first time.
func _ready():
	world.chairs.append(self)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func catchMan(body):
	body.position = Vector2(position)
	empty = false
	
func releaseMan():
	if man:
		man.queue_free()
		man = null
	if food:
		food.queue_free()
		food = null
	empty = true

func _on_chair_area_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	if body is Human: 
		catchMan(body)
		man = body

func _on_eat_area_body_entered(body):
	if body is Human:
		if body.food:	
			food = body.food as Food
			food.reparent(self)
			food.position = Vector2(20, 0)
		if man:
			man.startProgress()
			await man.pb.done
			releaseMan()
			add_score.emit()
